﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace weba.Sales
{
    public partial class PurchaseOrderHeaderListForCostUnitPriceAndPaymentManage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                var bll = mk.l.Sales_PurchaseOrderHeader.ToList();
                //设置经办人
               Accounts_Users currentUser = mk.l.Accounts_Users.FirstOrDefault();
                //设置根部门ID
             var bllDept = mk.l.Accounts_Department.ToList();
                this.lblRootDepartmentID.Text = bllDept.Where(j=>j.ClassID==(int.Parse(currentUser.DepartmentID))).FirstOrDefault().RootID.ToString();
                this.lblStrWhere.Text = " RootDepartmentID=" + this.lblRootDepartmentID.Text;
               // this.AspNetPager.RecordCount = bll.GetCount(this.lblStrWhere.Text);
                this.BindData();
                //设置分页页数
                this.txtPageSize.Attributes.Add("onkeydown", "javascript:SetFocusPageSize();");
                this.BindManufacturerID();
            }
        }
        private void BindManufacturerID()
        {
            var bll = mk.l.Sales_Manufacturer.ToList();
            var ds = bll.ToList();
            this.ddlManufacturerID.DataSource = ds;
            this.ddlManufacturerID.DataTextField = "ManufacturerName";
            this.ddlManufacturerID.DataValueField = "ManufacturerID";
            this.ddlManufacturerID.DataBind();
            ListItem li = new ListItem("不限", "0");
            this.ddlManufacturerID.Items.Add(li);
            this.ddlManufacturerID.Items.FindByValue("0").Selected = true;
        }

        /// <summary>
        /// 绑定订货单列表
        /// </summary>
        private void BindData()
        {
           var bll = mk.l.Sales_PurchaseOrderHeader.ToList();
         //   DataSet ds = bll.GetList(this.AspNetPager.PageSize, this.AspNetPager.CurrentPageIndex, this.lblStrWhere.Text, "1");
            this.myGridView.DataSource = bll;
            this.myGridView.DataBind();
        }

        /// <summary>
        /// 分页绑定数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void AspNetPager_PageChanged(object sender, EventArgs e)
        {
            this.BindData();
        }

        /// <summary>
        /// Handles the Click event of the btnSetPageSize control.设置分页页数
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void btnSetPageSize_Click(object sender, EventArgs e)
        {
            if (this.txtPageSize.Text.Trim() != "")
            {
                if (this.txtPageSize.Text.Trim() != "0")
                {
                   // this.AspNetPager.PageSize = int.Parse(this.txtPageSize.Text.Trim());
                    this.BindData();
                }
                else
                {
                   // this.AspNetPager.PageSize = 10;
                    this.BindData();
                }
            }
            else
            {
              //  this.AspNetPager.PageSize = 10;
                this.BindData();
            }
        }

        /// <summary>
        /// Handles the Click event of the btnSearch control.查询相关订单信息
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            this.lblStrWhere.Text = " RootDepartmentID=" + this.lblRootDepartmentID.Text;
            //设置查询条件
            //所有的条件都加and 最后再去掉就行了 tnnd早想到这个办法多好啊 不用写那么多if了
            StringBuilder strWhere = new StringBuilder();

            if (this.ddlManufacturerID.SelectedValue != "0")
            {
                strWhere.Append(" and ManufacturerID=" + this.ddlManufacturerID.SelectedValue);
            }

            if (this.txtOrderNumber.Text.Trim() != "")
            {
                strWhere.AppendFormat(" and OrderNumber like '%{0}%'", mk.InputText(this.txtOrderNumber.Text, 30));
            }

            if (strWhere.Length > 0)
            {
                //this.lblStrWhere.Text = strWhere.ToString().Substring(5, strWhere.Length - 5);
                this.lblStrWhere.Text += strWhere.ToString();
            }
          //  UART.BLL.Sales.PurchaseOrderHeader bll = new UART.BLL.Sales.PurchaseOrderHeader();
          //  this.AspNetPager.RecordCount = bll.GetCount(this.lblStrWhere.Text);
            this.BindData();
        }

        /// <summary>
        /// Handles the RowDataBound event of the myGridView control.显示厂家名称代码等信息
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.GridViewRowEventArgs"/> instance containing the event data.</param>
        protected void myGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label lblManufacturerName = (Label)(e.Row.FindControl("lblManufacturerName"));
                Label lblSubtotal = (Label)(e.Row.FindControl("lblSubtotal"));
                Label lblStatusPayment = (Label)(e.Row.FindControl("lblStatusPayment"));
                Label lblStatusArrival = (Label)(e.Row.FindControl("lblStatusArrival"));
               var bllManufacturer = mk.l.Sales_Manufacturer.ToList();
            Sales_Manufacturer mManufacturer = bllManufacturer.Where(h=>h.ManufacturerID==int.Parse(this.myGridView.DataKeys[e.Row.RowIndex].Values["ManufacturerID"].ToString())).FirstOrDefault();
                if (mManufacturer != null)
                {
                    lblManufacturerName.Text = mManufacturer.ManufacturerName;
                }
                //显示金额 付款状态 到货状态
                decimal decSubtotal = decimal.Parse(this.myGridView.DataKeys[e.Row.RowIndex].Values["SubTotal"].ToString());
                if (decSubtotal == 0)
                {
                    lblSubtotal.Text = "初始添加";
                }
                else
                {
                    lblSubtotal.Text = decSubtotal.ToString("c");
                }
                //显示付款状态
                string strStatusPayment = this.myGridView.DataKeys[e.Row.RowIndex].Values["StatusPayment"].ToString();
                if (strStatusPayment == "0")
                {
                    lblStatusPayment.Text = "未付款";
                    lblStatusPayment.ForeColor = System.Drawing.Color.Red;
                }
                else
                {
                    lblStatusPayment.Text = "已付款";
                    lblStatusPayment.ForeColor = System.Drawing.Color.Green;
                }
                //显示到货状态
                string strStatusArrival = this.myGridView.DataKeys[e.Row.RowIndex].Values["StatusArrival"].ToString();
                if (strStatusArrival == "0")
                {
                    lblStatusArrival.Text = "未全部到货";
                    lblStatusArrival.ForeColor = System.Drawing.Color.Red;
                }
                else
                {
                    lblStatusArrival.Text = "已全部到货";
                    lblStatusArrival.ForeColor = System.Drawing.Color.Green;
                }

                //让数字以文本形式表示
                e.Row.Cells[1].Attributes.Add("style", "vnd.ms-excel.numberformat:@");

            }
        }

    }
}
